package com.hejing.StringBufferAndStringBuilder;

import org.junit.Test;

/**
 * ClassName: StringBufferBuilderTest
 * Package: com.hejing.StringBufferAndStringBuilder
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-04-16 21:55
 * @Version: 1.0
 */
public class StringBufferBuilderTest {

    /**
     *
     *     -> (1) StringBuffer append(xx): 提供了很多的append()方法，用于进行字符串追加的方式拼接
     *     -> (2) StringBuffer delete(int start, int end) : 删除[start, end) 区间内的字符
     *     -> (3) StringBuffer deleteCharAt(int index): 删除index下标位置处的字符
     *     -> (4) StringBUffer replace(int start, int end, String str): 使用字符串str 替换下标范围为[start, end)的字符序列
     *     -> (5) void setCharAt(int index, char c): 使用字符c 替换 index位置上的字符
     *     -> (6) char charAt(index): 查找指定 index下标处的字符
     *     -> (7) StringBuffer insert(int index, xx): 在index下标处插入 xx
     *     -> (8) int length(): 返回存储的字符数据的长度
     *     -> (9) StringBuffer reverse(): 字符串反转
     */
    @Test
    public void test01(){
        StringBuilder sBuilder = new StringBuilder();
        System.out.println(sBuilder.append("abc").append("123").append("def"));  // abc123def

        StringBuilder sBuilder1 = new StringBuilder("hello");
        sBuilder1.insert(3, "你好世界");
        System.out.println(sBuilder1);  // hel你好世界lo

        System.out.println(sBuilder1.reverse());    // ol界世好你leh
        System.out.println(sBuilder1);              // ol界世好你leh

        System.out.println(sBuilder.length());  // 9


    }

    /**
     * -> (10) int indexOf(String str): 在当前字符序列中查询str的第一次出现下标
     * -> (11) int indexOf(String str, int fromIndex):在当前字符序列[fromIndex,最后]中查询str的第一次出现的下标
     * -> (12) int lastIndexOf(String str): 在当前字符序列中查询str的最后一次出现下标
     * -> (13) int lastIndexOf(String str, int fromlndex): 在当前字符序列[fromindex,最后]中查询str的最后一次出现的下标
     * -> (14) String substring(int start): 截取当前字符序列[start,最后]
     * -> (15) String substring(int start, int end): 截取当前字符序列[start,end)
     * -> (16) String toString: 返回此序列中数据的字符串表示形式
     * -> (17) void setLength(int newLength) :设置当前字符序列长度为newLength
     *
     */
    @Test
    public void test02(){
        // StringBuilder.setLength()
        StringBuilder sBuilder = new StringBuilder("hello");
//        sBuilder.setLength(2);
//        System.out.println(sBuilder);  // he

        sBuilder.setLength(10);
        System.out.println(sBuilder);  // hello空格空格空格空格空格

        StringBuilder sBuilder1 = new StringBuilder("helloworldhellojava");
        System.out.println(sBuilder1.lastIndexOf("he", 9));


    }

    /**
     * String StringBuilder StringBuffer 执行效率测试
     */
    @Test
    public void test03(){

        // 初识设置
        long startTime = 0L;
        long endTime = 0L;
        String text = "";
        StringBuffer sBuffer = new StringBuffer("");
        StringBuilder sBuilder = new StringBuilder("");

        // 开始对比:
        startTime = System.currentTimeMillis();
        for(int i = 0; i < 20000; i++) {
            sBuffer.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer的执行时间是: " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for(int i = 0; i < 20000; i++) {
            sBuilder.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer的执行时间是: " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for(int i = 0; i < 20000; i++) {
            text += String.valueOf(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer的执行时间是: " + (endTime - startTime));
    }

}






























